package cz.muni.fi.pa165.sportclubmanager.service;

import cz.muni.fi.pa165.sportclubmanager.dao.PlayerDAO;
import cz.muni.fi.pa165.sportclubmanager.dao.PlayerOnRosterDAO;
import cz.muni.fi.pa165.sportclubmanager.dto.AgeCategoryDTO;
import cz.muni.fi.pa165.sportclubmanager.dto.AgeCategoryDTOMapping;
import cz.muni.fi.pa165.sportclubmanager.dto.PlayerDTO;
import cz.muni.fi.pa165.sportclubmanager.dto.PlayerDTOMapping;
import cz.muni.fi.pa165.sportclubmanager.entity.Player;
import cz.muni.fi.pa165.sportclubmanager.entity.PlayerOnRoster;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@WebService(endpointInterface = "cz.muni.fi.pa165.sportclubmanager.service.PlayerService", serviceName="PlayerWebService")
public class PlayerServiceImpl implements PlayerService {
    
    @Autowired
    private PlayerDAO playerDAO;
    
    @Autowired
    private PlayerOnRosterDAO rosterDAO;
           
    @Override
    @Transactional
    @Secured({"ROLE_ADMIN"})
    public void create(PlayerDTO p) {
        playerDAO.create(PlayerDTOMapping.toEntity(p));        
    }

    @Override
    @Transactional
    @Secured({"ROLE_ADMIN"})
    public void update(PlayerDTO p) {
        playerDAO.update(PlayerDTOMapping.toEntity(p));                
    }

    @Override
    @Transactional
    @Secured({"ROLE_ADMIN"})
    public void remove(PlayerDTO p) {
        Player player = PlayerDTOMapping.toEntity(p);
        List<PlayerOnRoster> playerOnRosters = rosterDAO.findAllWithPlayer(player);
        for (PlayerOnRoster roster : playerOnRosters) {   
            rosterDAO.remove(roster);
        }
        playerDAO.remove(player);                        
    }

    @Override
    @Transactional  
    @Secured({"ROLE_USER","ROLE_ADMIN"})        
    public PlayerDTO find(Long id) {
        Player p = playerDAO.find(id);                                        
        
        return PlayerDTOMapping.toDto(p);
    }

    @Override
    @Transactional    
    @Secured({"ROLE_USER","ROLE_ADMIN"})        
    public List<PlayerDTO> findAll() {
        List<Player> list = playerDAO.findAll();
        List<PlayerDTO> dtoList = new ArrayList<PlayerDTO>();
                
        for (Iterator i = list.iterator(); i.hasNext();) {            
            dtoList.add(PlayerDTOMapping.toDto((Player) i.next()));
        }
        return dtoList;
    }
        
    @Override
    @Transactional     
    @Secured({"ROLE_USER","ROLE_ADMIN"})        
    public List<PlayerDTO> getPlayersFor(AgeCategoryDTO ac) {
        List<Player> list = playerDAO.getPlayersFor(AgeCategoryDTOMapping.toEntity(ac));
        List<PlayerDTO> dtoList = new ArrayList<>(list.size());

        for (Player player : list) {
            dtoList.add(PlayerDTOMapping.toDto(player));
        }
        return dtoList;
    }
}
